package com.wmcsk.dl.dl;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.WindowManager.LayoutParams;

import com.wmcsk.dl.dl.internal.DLAttachable;
import com.wmcsk.dl.dl.internal.DLPluginManager;
import com.wmcsk.dl.dl.internal.DLProxyImpl;
import com.wmcsk.dl.dl.utils.DL;


public class DLProxyFragmentActivity extends FragmentActivity implements DLAttachable {
	private static final String TAG = DLProxyFragmentActivity.class.getName();
	protected DLPlugin mRemoteActivity;
    private DLProxyImpl impl = new DLProxyImpl(this);
    public boolean visibleActivity=true; //默认插件可见可跳转
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setVisibleActivity(true); //新界面默认可见
        try {
            impl.onCreate(getIntent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager) {
        DL.e("绑定："+remoteActivity);
        mRemoteActivity = remoteActivity;
    }

    @Override
    public AssetManager getAssets() {
    		return impl.getAssets() == null ? super.getAssets() : impl.getAssets();
    }

    @Override
    public Resources getResources() {
    		return impl.getResources() == null ? super.getResources() : impl.getResources();
    }

    @Override
    public Theme getTheme() {
    		return impl.getTheme() == null ? super.getTheme() : impl.getTheme();
    }

    @Override
    public ClassLoader getClassLoader() {
    		return impl.getClassLoader();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mRemoteActivity.onActivityResult(requestCode, resultCode, data);
        DL.e(TAG, "onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStart() {
        mRemoteActivity.onStart();
        DL.e(TAG, "onStart");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        mRemoteActivity.onRestart();
        DL.e(TAG, "onRestart");
        super.onRestart();
    }

    @Override
    protected void onResume() {
        mRemoteActivity.onResume();
        setVisibleActivity(true); //设置界面再次可见
        DL.e(TAG, "onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        mRemoteActivity.onPause();
        DL.e(TAG, "onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        mRemoteActivity.onStop();
        DL.e(TAG, "onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mRemoteActivity.onDestroy();
        DL.e(TAG, "onDestroy");
        super.onDestroy();
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
			mRemoteActivity.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
			mRemoteActivity.onRestoreInstanceState(savedInstanceState);
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
			mRemoteActivity.onNewIntent(intent);
        super.onNewIntent(intent);
    }

    @Override
    public void onBackPressed() {
        mRemoteActivity.onBackPressed();
        if(mRemoteActivity.isBackFinish()){
            super.onBackPressed();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        return mRemoteActivity.onTouchEvent(event);

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        super.onKeyUp(keyCode, event);
        return mRemoteActivity.onKeyUp(keyCode, event);
    }

    @Override
    public void onWindowAttributesChanged(LayoutParams params) {
            DL.e("mRemot="+mRemoteActivity);
            mRemoteActivity.onWindowAttributesChanged(params);
        super.onWindowAttributesChanged(params);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        mRemoteActivity.onWindowFocusChanged(hasFocus);
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
			mRemoteActivity.onCreateOptionsMenu(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mRemoteActivity.onOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }

	@Override
	public String getPluginPackageName() {
		String pluginPackageName=mRemoteActivity.getPackageName();
		if(TextUtils.isEmpty(pluginPackageName)){
			pluginPackageName=getPackageName();
		}
		return pluginPackageName;
	}
    @Override
    public Activity getPluginActivity() {
        return (Activity)mRemoteActivity;
    }

    /**
     * 标识插件activity不可见
     * @return
     */
    @Override
    public boolean isVisibleActivity() {
        return visibleActivity;
    }

    @Override
    public void setVisibleActivity(boolean isVisibleActivity) {
        this.visibleActivity=isVisibleActivity;
    }

}
